using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using ProtoModels;
using UnityEngine;
using tar_cs;

public class AdBundle : BehaviourSingleton<AdBundle>
{
	public interface AdBundleLoadingListener
	{
		void OnBundleLoadingComplete();

		void OnRequestStarted(WWW request);
	}

	public class Bundle
	{
		public int lastModifiedUnixTimestamp;

		public Texture2D texture;

		public string text;

		public InAppAdsModel model;
	}

	protected bool useFastCache;

	public string file;

	private Bundle bundle;

	private bool inRequest;

	private float timeToNextPoll;

	private const float minimalTimeBetweenPolls = 30f;

	public const string adsTextureAtlasFilename = "ads.txt";

	public const string adsTextureFilename = "ads.png";

	public const string adsResourcesName = "ads";

	public const string adsConfigFilename = "config.bytes";

	public int columnsTip
	{
		get
		{
			if (bundle == null || bundle.model == null)
			{
				return 0;
			}
			return bundle.model.columnsTip;
		}
	}

	public static float GetWeight(AdConfigModel ad)
	{
		AdLogModel adLogModel = Singleton<InAppAdLog>.Instance.LogForAd(ad);
		int num = 0;
		if (ad.impressionsToMinWeight > 0)
		{
			Mathf.Max((float)adLogModel.timesShown / (float)ad.impressionsToMinWeight, num);
		}
		if (ad.clicksToMinWeight > 0)
		{
			Mathf.Max((float)adLogModel.timesClicked / (float)ad.clicksToMinWeight, num);
		}
		return ad.weight - (ad.weight - ad.minWeight) * (float)num;
	}

	public virtual string GetAdBundleName()
	{
		return ConfigBase.instance.inAppAdsName;
	}

	public virtual bool isNetworkAvailable()
	{
		return GGSupportMenu.instance.isNetworkConnected();
	}

	public string getCampaignName()
	{
		string result = "camp";
		if (bundle != null && bundle.model != null && !string.IsNullOrEmpty(bundle.model.campaignName))
		{
			result = bundle.model.campaignName;
		}
		return result;
	}

	public virtual void AnaliticReportAdShown(AdConfigModel ad)
	{
	}

	public virtual void AnalyticReportAdClick(AdConfigModel ad)
	{
	}

	public virtual void AnalyticReportAdClick(MessageAdConfigModel ad)
	{
	}

	public virtual bool AdClick(AdConfigModel ad)
	{
		return AdClick(ad.linkType, ad.actionLink);
	}

	public virtual bool AdClick(ActionLinkType linkType, string actionLink)
	{
		switch (linkType)
		{
		case ActionLinkType.AppLink:
			GGSupportMenu.instance.OpenStoreUrl(actionLink, ConfigBase.instance.rateProvider);
			break;
		case ActionLinkType.PublisherLink:
			GGSupportMenu.instance.OpenPublisherStoreUrl(actionLink, ConfigBase.instance.rateProvider);
			break;
		}
		return isNetworkAvailable();
	}

	public Bundle GetBundle()
	{
		Debug.Log("Get Bundle");
		if (bundle == null)
		{
			LoadBundleFromCache();
		}
		if (bundle == null)
		{
			LoadBundleFromDefault();
		}
		return bundle;
	}

	private void LoadBundleFromCache()
	{
		try
		{
			string path = adBundleCachedFilename();
			Debug.Log("PERSISTENT Path: " + Application.persistentDataPath);
			if (GGFileIO.instance.FileExists(path))
			{
				using (Stream stream = GGFileIO.instance.FileReadStream(path))
				{
					bundle = BundleFromStream(stream);
				}
				Debug.Log("AdBundle loaded from Cache");
			}
		}
		catch (Exception ex)
		{
			Debug.Log("Exception " + ex.ToString());
		}
	}

	private void LoadBundleFromDefault()
	{
		InAppAdsConfig inAppAdsConfig = Resources.Load(GetAdBundleName(), typeof(InAppAdsConfig)) as InAppAdsConfig;
		Debug.Log(string.Concat("Load Bundle From Default ", inAppAdsConfig, " name ", GetAdBundleName()));
		Bundle bundle = new Bundle();
		if (inAppAdsConfig != null)
		{
			bundle.model = inAppAdsConfig.toProtoModel();
		}
		else
		{
			bundle.model = new InAppAdsModel();
		}
		bundle.texture = Resources.Load("ads", typeof(Texture2D)) as Texture2D;
		bundle.text = (Resources.Load("ads", typeof(TextAsset)) as TextAsset).text;
		Debug.Log("Loaded Bundle Model Exists " + (bundle.model != null));
		if (bundle.model != null)
		{
			Debug.Log("Bundle size " + bundle.model.adConfig.Count);
		}
		this.bundle = bundle;
	}

	public void ReportShown(MessageAdConfigModel ad)
	{
		InAppAdLog inAppAdLog = Singleton<InAppAdLog>.Instance;
		inAppAdLog.LogForAd(ad).timesShown++;
		inAppAdLog.Save();
	}

	public void ReportShownAds(List<AdConfigModel> ads)
	{
		InAppAdLog inAppAdLog = Singleton<InAppAdLog>.Instance;
		foreach (AdConfigModel ad in ads)
		{
			inAppAdLog.LogForAd(ad).timesShown++;
			AnaliticReportAdShown(ad);
		}
		inAppAdLog.Save();
	}

	public void ReportAdClick(MessageAdConfigModel ad)
	{
		if (AdClick(ad.linkType, ad.actionLink))
		{
			Singleton<InAppAdLog>.Instance.LogForAd(ad).timesClicked++;
			Singleton<InAppAdLog>.Instance.Save();
			AnalyticReportAdClick(ad);
		}
	}

	public void ReportAdClick(AdConfigModel ad)
	{
		if (AdClick(ad))
		{
			Singleton<InAppAdLog>.Instance.LogForAd(ad).timesClicked++;
			Singleton<InAppAdLog>.Instance.Save();
			AnalyticReportAdClick(ad);
		}
	}

	public MessageAdConfigModel InterstitialAd()
	{
		if (!GGSupportMenu.instance.isNetworkConnected())
		{
			return null;
		}
		if (bundle == null || bundle.model == null || bundle.model.adMessageConfig == null)
		{
			return null;
		}
		return null;
	}

	public int hoursToSeconds(int hours)
	{
		return hours * 60 * 60;
	}

	private void CallOnComplete(AdBundleLoadingListener listener)
	{
		if (listener == null)
		{
			return;
		}
		try
		{
			listener.OnBundleLoadingComplete();
		}
		catch
		{
		}
	}

	private void CallRequestStarted(AdBundleLoadingListener listener, WWW request)
	{
		if (listener == null)
		{
			return;
		}
		try
		{
			listener.OnRequestStarted(request);
		}
		catch
		{
		}
	}

	public void TryToRefreshBundle(bool force = false, AdBundleLoadingListener listener = null)
	{
		if (!isNetworkAvailable())
		{
			CallOnComplete(listener);
			return;
		}
		long num = unixTimestampNow() - Singleton<InAppAdLog>.Instance.lastSuccessfullPollUnixTimestamp();
		long num2 = hoursToSeconds(12);
		string text = "http://www.giraffe-games.com/inappads/adbundle.php";
		Bundle bundle = GetBundle();
		if (bundle != null && bundle.model != null)
		{
			num2 = bundle.model.secondsTillPoll;
			if (!string.IsNullOrEmpty(bundle.model.serverURL))
			{
				text = bundle.model.serverURL;
			}
		}
		if (num2 > num && !force)
		{
			CallOnComplete(listener);
			return;
		}
		if (ConfigBase.instance.useHttpsForAdBundles && text.StartsWith("http://"))
		{
			text = text.Replace("http://", "https://");
		}
		text = text + "?app=" + GetAdBundleName();
		text = text + "&t=" + unixTimestampOfMyAdBundle();
		Debug.Log("URL " + text);
		LoadFromURL(text, listener);
	}

	private void LoadFromURL(string url, AdBundleLoadingListener listener = null)
	{
		if (inRequest)
		{
			CallOnComplete(listener);
		}
		else
		{
			StartCoroutine(DoLoadFromURL(url, listener));
		}
	}

	private string adBundleCachedFilename()
	{
		return GetAdBundleName() + ".zip";
	}

	private int unixTimestampOfMyAdBundle()
	{
		string path = adBundleCachedFilename();
		if (!GGFileIO.instance.FileExists(path))
		{
			return 0;
		}
		return unixTimestamp(GGFileIO.instance.LastWriteTimeUTC(path));
	}

	private int unixTimestampNow()
	{
		return (int)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
	}

	private int unixTimestamp(DateTime time)
	{
		return (int)time.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
	}

	private string loadText(Stream reader)
	{
		byte[] array = new byte[reader.Length];
		reader.Read(array, 0, (int)reader.Length);
		return Encoding.UTF8.GetString(array, 0, array.Length);
	}

	private InAppAdsModel loadModel(Stream reader)
	{
		InAppAdsModel inAppAdsModel = new InAppAdsModel();
		byte[] buffer = new byte[reader.Length];
		reader.Read(buffer, 0, (int)reader.Length);
		using (MemoryStream source = new MemoryStream(buffer, false))
		{
			ProtoSerializer protoSerializer = new ProtoSerializer();
			return protoSerializer.Deserialize(source, null, typeof(InAppAdsModel)) as InAppAdsModel;
		}
	}

	private InAppAdsModel loadModel(byte[] buffer)
	{
		InAppAdsModel inAppAdsModel = new InAppAdsModel();
		using (MemoryStream source = new MemoryStream(buffer, false))
		{
			ProtoSerializer protoSerializer = new ProtoSerializer();
			return protoSerializer.Deserialize(source, null, typeof(InAppAdsModel)) as InAppAdsModel;
		}
	}

	private void Update()
	{
		if (timeToNextPoll <= 0f)
		{
			timeToNextPoll = 30f;
			TryToRefreshBundle();
		}
		timeToNextPoll -= Time.deltaTime;
	}

	private Texture2D loadTexture(Stream reader)
	{
		Texture2D texture2D = new Texture2D(0, 0);
		byte[] array = new byte[reader.Length];
		reader.Read(array, 0, (int)reader.Length);
		texture2D.LoadImage(array);
		return texture2D;
	}

	private Texture2D loadTexture(byte[] buffer)
	{
		Texture2D texture2D = new Texture2D(0, 0);
		texture2D.LoadImage(buffer);
		return texture2D;
	}

	private IEnumerator DoLoadFromURL(string url, AdBundleLoadingListener listener = null)
	{
		if (inRequest)
		{
			CallOnComplete(listener);
			yield break;
		}
		inRequest = true;
		url = url + "&t=" + unixTimestampOfMyAdBundle();
		Debug.Log("AdURL: " + url);
		WWW www = new WWW(url);
		CallRequestStarted(listener, www);
		yield return www;
		inRequest = false;
		if (string.IsNullOrEmpty(www.error) || www.error.Contains("406"))
		{
			Singleton<InAppAdLog>.Instance.setLastSuccessfullPollUnixTimestamp(unixTimestampNow());
		}
		else
		{
			Singleton<InAppAdLog>.Instance.setLastSuccessfullPollUnixTimestamp(unixTimestampNow());
		}
		if (!string.IsNullOrEmpty(www.error))
		{
			Debug.Log("Error " + www.error);
			CallOnComplete(listener);
			yield break;
		}
		try
		{
			byte[] bytes = www.bytes;
			using (MemoryStream stream = new MemoryStream(bytes, false))
			{
				Bundle bundle = BundleFromStream(stream);
				if (bundle != null)
				{
					this.bundle = bundle;
				}
				string path = adBundleCachedFilename();
				GGFileIO.instance.Write(path, bytes);
			}
			Debug.Log("Successfull loading of AdBundle");
			CallOnComplete(listener);
		}
		catch
		{
			CallOnComplete(listener);
		}
	}

	public Bundle BundleFromStream(Stream stream)
	{
		Debug.Log("Tar Bundle from stream");
		Bundle bundle = new Bundle();
		TarReader tarReader = new TarReader(stream);
		while (tarReader.MoveNext(false))
		{
			string fileName = tarReader.FileInfo.FileName;
			Debug.Log("FILENAME: " + fileName + " size " + tarReader.FileInfo.SizeInBytes);
			using (MemoryStream memoryStream = new MemoryStream())
			{
				tarReader.Read(memoryStream);
				if (fileName.EndsWith("png"))
				{
					bundle.texture = loadTexture(memoryStream.ToArray());
					Debug.Log("texture " + (bundle.texture != null));
				}
				else if (fileName.EndsWith("txt"))
				{
					byte[] array = memoryStream.ToArray();
					bundle.text = Encoding.UTF8.GetString(array, 0, array.Length);
					Debug.Log("Text " + bundle.text);
				}
				else if (fileName.EndsWith("bytes"))
				{
					Debug.Log("MODEL BUFFER " + Convert.ToBase64String(memoryStream.ToArray()));
					bundle.model = loadModel(memoryStream.ToArray());
					Debug.Log("model server " + bundle.model.serverURL);
				}
			}
		}
		return bundle;
	}
}
